package com.yanqu.road.server.cmd.travel;

import com.yanqu.road.entity.player.UserTravel;
import com.yanqu.road.entity.travel.TravelGain;
import com.yanqu.road.entity.travel.TravelResult;
import com.yanqu.road.logic.pb.TravelPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.travel.TravelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.travel.TravelModule;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_TRAVEL_GO, desc = "出游")
public class TravelGoCmd extends AbstractCommand {

	private int travelType;

	// CMD处理前的赚速
	private String preEarnSpeed;

	private TravelResult travelResult;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		TravelProto.TravelGoReqMsg reqMsg = TravelProto.TravelGoReqMsg.parseFrom(packet.getBytes());
		travelType = reqMsg.getTravelType();
		// TODO  更客户端同步协议
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		preEarnSpeed = player.getUserInfo().getEarnSpeed().toString();

		travelResult = player.getModule(TravelModule.class).travel(travelType);
	}

	@Override
	public void sendData(GamePlayer player) {
//		TravelProto.TravelGoRespMsg.Builder respMsg = TravelProto.TravelGoRespMsg.newBuilder();
//		respMsg.setRet(travelResult.getRet());
//		UserTravel userTravel = player.getModule(TravelModule.class).getUserTravel();
//		if(0 == travelResult.getRet()) {
//			for (TravelGain travelGain : travelResult.getTravelGainList()) {
//				respMsg.addEventIdList(travelGain.getEventId());
//			}
//		}else {
//			if(null != userTravel) {
//				respMsg.addAllEventIdList(userTravel.getEventIdList());
//			}
//		}
//
//		if(null != userTravel) {
//			respMsg.setCityId(userTravel.getCityId());
//		}
//
//		respMsg.setPreEarnSpeed(preEarnSpeed);
//		respMsg.setNextEarnSpeed(player.getUserInfo().getEarnSpeed().toString());
//
//		player.sendPacket(Protocol.U_TRAVEL_GO, respMsg);
//		if (travelResult.getRet() == GameErrorCode.E_TRAVEL_POWER_NO_ENOUGH) {
//			player.getModule(TravelModule.class).syncTravelPower();
//		}
		//封装一下游历返回
		UserTravel userTravel = player.getModule(TravelModule.class).getUserTravel();
		TravelProto.TravelGoRespMsg.Builder respMsg = TravelPb.createTravelGoRespMsg(travelResult, userTravel, preEarnSpeed, player.getUserInfo().getEarnSpeed().toString());
		player.sendPacket(Protocol.U_TRAVEL_GO, respMsg);
		if (travelResult.getRet() == GameErrorCode.E_TRAVEL_POWER_NO_ENOUGH) {
			player.getModule(TravelModule.class).syncTravelPower();
		}
	}
}
